package com.hhl.shardingjdbc.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhl.shardingjdbc.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hhl.shardingjdbc.entity.Order;
import com.hhl.shardingjdbc.mapper.OrderMapper;

@RestController
public class OrderController {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    IOrderService orderService;

    @GetMapping("/list")
    List<Order> list() {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<Order>();
        return orderMapper.selectList(queryWrapper);
    }

    @GetMapping
    public IPage<Order> getList(
            @RequestParam(required = false, defaultValue = "1") Integer current,
            @RequestParam(required = false, defaultValue = "8") Integer size,String userName) {
        return orderService.getPage(new Page<>(current, size),userName);
    }

    @GetMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public String insertUser() {
        for (int i = 1; i <= 10; i++) {
            Order order = new Order();
            order.setId(i);
            order.setOrderId(i);
            order.setUserId(i);
            order.setUserName("hhl");

            orderMapper.insert(order);

        }
        return "success";
    }
}
